home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15086 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: scopevle.demon.co.uk!gsmith
  2. From: "Gary J. Smith" <gsmith@scopevle.demon.nl>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP: Overloading [] operator for multidimensional array of objects.
  5. Date: Wed, 3 Apr 1996 16:56:40 +0100
  6. Organization: ScopeVale Ltd. (Amsterdam)
  7. Distribution: world
  8. Message-ID: <o+xjVJA4+pYxEwrx@scopevle.demon.co.uk>
  9. References: <4j6p2v$dhg@vixen.cso.uiuc.edu>
  10. NNTP-Posting-Host: scopevle.demon.co.uk
  11. X-NNTP-Posting-Host: scopevle.demon.co.uk
  12. MIME-Version: 1.0
  13. X-Newsreader: Turnpike (evaluation) Version 1.12 <8zaiLiZKAVASB6YSBmEMpYSBBk>
  14.  
  15. In article <4j6p2v$dhg@vixen.cso.uiuc.edu>, "Edgar L. Torres" <e-
  16. torres@uiuc.edu> writes
  17. >Hi!
  18. >
  19. >       I would like to specify the overload of [], such that I may access
  20. >a multidimensional array of objects. As an example, given I have a
  21. >class
  22. >EXAMCLASS and I wish to dynamically allocate a three-dimensional array
  23. >of
  24. >objects:
  25. >       EXAMCLASS object1***;
  26. >       ...
  27. >       object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
  28. >I know the above is sacrilege to C++ purists, but bare with me, since
  29. >the malloc
  30. >does work correctly in C++.  Now, what I want is to be able to:
  31. >       object1[arg1][arg2][arg3].obj1Var1 = something;
  32. >OR
  33. >       something = object1[arg1][arg2][arg3].obj1Var1;
  34. >In general, I know I have to include three friend operator in the
  35. >definition of
  36. >class EXAMCLASS, and then define them outside:
  37. >       EXAMCLASS** operator[](EXAMCLASS*** C, int i){
  38. >               return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
  39. >       EXAMCLASS* operator[](EXAMCLASS** C, int i){
  40. >               return ((EXAMCLASS *)(C + (i * dim3sz))); }
  41. >       EXAMCLASS operator[](EXAMCLASS* C, int i){
  42. >               return *(C + i); }
  43. >I know that the above does not work, but it does present the basic
  44. >idea of what
  45. >I am trying to do.  If anybody has the knowledge to help with the
  46. >specifics of 
  47. >syntax and argument rules, please HELP ME!  Somebody suggested
  48. >replacing the *
  49. >with &, but I am not sure what EXAMCLASS&& means??
  50. >
  51. >Thanks in advance,
  52. >Edgar
  53. >
  54. >
  55.  
  56. As a short cut, why not overload the () operator - it can take as many (
  57. and differing) parameter types as you want ? This can then return a
  58. reference to something that you can amend.
  59.  
  60. (Maybe I missed the point, but I had this problem so this is what I
  61. did).
  62.  
  63. Gary.
  64.  
  65. ---------------------------------------------------
  66.         Gary J. Smith
  67.         gsmith@scopevle.demon.co.uk
  68.         gsmith@scopevle.demon.nl
  69.         
  70. ---------------------------------------------------
  71.  
  72. Turnpike evaluation. For information, see http://www.turnpike.com/
  73.